using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using NTTHAO.Common.BL;
using NTTHAO.CommonWeb;

public partial class admin_webparts_AdminUser_AdminUserInput : System.Web.UI.UserControl
{
    public NTTHAO.Common.BL.AdminUser _EditingUser = null;
    public string _ErrorMessage = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        this.chkActive.Text = LanguageUtility.GetLanguageText("ACTIVE", Session);
        this.btnSave.Text = LanguageUtility.GetLanguageText("SAVE", Session);
        this.btnSave2.Text = LanguageUtility.GetLanguageText("SAVE", Session);

        if (!IsPostBack)
        {
            this.cmbUserType.Items.Add(new ListItem(LanguageUtility.GetLanguageText("NOMAL_USER", Session), "1"));
            this.cmbUserType.Items.Add(new ListItem(LanguageUtility.GetLanguageText("ADMIN", Session), "0"));

            string strAction = Request.QueryString["action"];
            string strID = Request.QueryString["id"];

            NTTHAO.Common.BL.AdminUser loggedUser;
            try
            {
                loggedUser = new NTTHAO.Common.BL.AdminUser(Context.User.Identity.Name);
                if (loggedUser.ID < 1)
                {
                    Response.Redirect("?type=user&action=list");
                    return;
                }
            }
            catch (Exception)
            {
                Response.Redirect("?type=user&action=list");
                return;
            }

            if (strAction != null)
            {
                if (strAction.Equals("edit", StringComparison.OrdinalIgnoreCase))
                {
                    txtUserName.Enabled = false;
                    int id;
                    try
                    {
                        id = Int32.Parse(strID);
                        NTTHAO.Common.BL.AdminUser adminUser = new NTTHAO.Common.BL.AdminUser(id);

                        if ((loggedUser.Type != 0 && loggedUser.ID != adminUser.ID))
                        {
                            Response.Redirect("?type=user&action=list");
                            return;
                        }
                        if (loggedUser.Type != 0 || loggedUser.ID == adminUser.ID)
                        {
                            cmbUserType.Enabled = false;
                            chkActive.Enabled = false;
                        }

                        if (adminUser.ID > 0)
                        {
                            txtUserName.Text = adminUser.UserName;
                            chkActive.Checked = adminUser.Active;
                            cmbUserType.SelectedValue = adminUser.Type.ToString();
                            userID.Value = adminUser.ID.ToString();

                            _EditingUser = adminUser;
                        }
                    }
                    catch (Exception)
                    {
                        Response.Redirect("?type=user&action=list");
                    }
                }
                else if (strAction.Equals("add", StringComparison.OrdinalIgnoreCase))
                {
                    if ((loggedUser.Type != 0))
                    {
                        Response.Redirect("?type=user&action=list");
                        return;
                    }
                    txtUserName.Text = "";
                    chkActive.Checked = true;
                    userID.Value = "";

                    txtUserName.Enabled = true;
                }
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string strAction = Request.QueryString["action"];
        string strID = Request.QueryString["id"];

        NTTHAO.Common.BL.AdminUser loggedUser = new NTTHAO.Common.BL.AdminUser(Context.User.Identity.Name);
        if (loggedUser.ID < 1)
        {
            Response.Redirect("?type=user&action=list");
            return;
        }

        if (strAction != null)
        {
            if (strAction.Equals("edit", StringComparison.OrdinalIgnoreCase))
            {
                int id;
                try
                {
                    id = Int32.Parse(userID.Value.Trim());
                    NTTHAO.Common.BL.AdminUser adminUser = new NTTHAO.Common.BL.AdminUser(id);

                    if (adminUser.ID > 0)
                    {
                        if ((loggedUser.Type != 0 && loggedUser.ID != adminUser.ID))
                        {
                            Response.Redirect("?type=user&action=list");
                            return;
                        }

                        if (loggedUser.Type == 0 && loggedUser.ID != adminUser.ID)
                        {
                            adminUser.Type = Int32.Parse(cmbUserType.SelectedValue);
                            adminUser.Active = chkActive.Checked;
                        }
                        if (encryptedPassword.Value.Length > 0)
                        {
                            adminUser.Password = encryptedPassword.Value.Trim();
                        }
                        adminUser.Update();
                        Response.Redirect("?type=user&action=list");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("?type=user&action=list");
                }
            }
            else if (strAction.Equals("add", StringComparison.OrdinalIgnoreCase))
            {
                if ((loggedUser.Type != 0))
                {
                    Response.Redirect("?type=user&action=list");
                    return;
                }
                NTTHAO.Common.BL.AdminUser adminUser = new NTTHAO.Common.BL.AdminUser(txtUserName.Text.Trim());
                if (adminUser.ID > 0)
                {
                    _ErrorMessage = LanguageUtility.GetLanguageText("EXISTING_USERNAME", Session);
                }
                else
                {
                    adminUser.UserName = txtUserName.Text.Trim();
                    adminUser.Password = encryptedPassword.Value.Trim();
                    adminUser.Type = Int32.Parse(cmbUserType.SelectedValue);
                    adminUser.Active = chkActive.Checked;
                    adminUser.Insert();
                    Response.Redirect("?type=user&action=list");
                }
            }
        }
    }
}
